package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.CenterMapper;
import com.ruoyi.cderp.domain.Center;
import com.ruoyi.cderp.service.ICenterService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 仓库Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class CenterServiceImpl implements ICenterService
{
    @Autowired
    private CenterMapper centerMapper;

    LoginUser loginUser = null;

    /**
     * 查询仓库
     *
     * @param id 仓库主键
     * @return 仓库
     */
    @Override
    public Center selectCenterById(String id)
    {
        return centerMapper.selectCenterById(id);
    }

    /**
     * 查询仓库列表
     *
     * @param center 仓库
     * @return 仓库
     */
    @Override
    public List<Center> selectCenterList(Center center)
    {
        return centerMapper.selectCenterList(center);
    }

    /**
     * 新增仓库
     *
     * @param center 仓库
     * @return 结果
     */
    @Override
    public int insertCenter(Center center)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formattedDate = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
        center.setId("10" + formattedDate+randomNumber);
        center.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        center.setCreateById(loginUser.getUser().getUserId().toString());
        center.setCreateBy(loginUser.getUser().getUserName());
        center.setCreateTime(DateUtils.getNowDate());
        return centerMapper.insertCenter(center);
    }

    /**
     * 修改仓库
     *
     * @param center 仓库
     * @return 结果
     */
    @Override
    public int updateCenter(Center center)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        center.setUpdateById(loginUser.getUser().getUserId().toString());
        center.setUpdateBy(loginUser.getUser().getUserName());
        center.setUpdateTime(DateUtils.getNowDate());
        return centerMapper.updateCenter(center);
    }

    /**
     * 批量删除仓库
     *
     * @param ids 需要删除的仓库主键
     * @return 结果
     */
    @Override
    public int deleteCenterByIds(String[] ids)
    {
        return centerMapper.deleteCenterByIds(ids);
    }

    /**
     * 删除仓库信息
     *
     * @param id 仓库主键
     * @return 结果
     */
    @Override
    public int deleteCenterById(String id)
    {
        return centerMapper.deleteCenterById(id);
    }
}
